﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Util;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.FormPlugin.XHX
{
    [Description("XHXOnBeforeWebApiOperation"), HotUpdate]
    public class XHXOnBeforeWebApiOperation : AbstractBillPlugIn
    {
        public override void OnBeforeWebApiOperation(AfterWebApiOperationArgs e)
        {
            base.OnBeforeWebApiOperation(e);
            if (e.WebContext.FormOperation.Operation == "Save")
            {

                var requestDto = e.WebContext.RequestDTO;
                JObject jobject = JObject.Parse(Convert.ToString(requestDto));
                if (jobject["Model"]["FSalerId"]["FNumber"].ToString() == "")
                {
                    jobject["Model"]["FSalerId"]["FNumber"] = "";
                }
                string salesno = jobject["Model"]["FBillNo"].ToString();
                string F_SOType = jobject["Model"]["F_SOType"]["FNumber"].ToString();
                jobject["Model"]["F_SOType"].Parent.Remove();
                jobject["Model"].First.AddAfterSelf(new JProperty("F_SOType", F_SOType));
                for (int i = 0; i < jobject["Model"]["FSaleOrderEntry"].Count(); i++)
                {
                    string material = jobject["Model"]["FSaleOrderEntry"][i]["FMaterialId"]["FNumber"].ToString();
                    var FMtoNo = new JProperty("FMtoNo", salesno + "-" + (i + 1).ToString());
                    var ForgID = new JProperty("FStockOrgId", JsonConvert.DeserializeObject("{\"FNumber\":\"\"}"));

                    jobject["Model"]["FSaleOrderEntry"][i].Last.AddAfterSelf(FMtoNo);
                    jobject["Model"]["FSaleOrderEntry"][i].First.AddAfterSelf(ForgID);
                    string ParentUnit = jobject["Model"]["FSaleOrderEntry"][i]["FUnitID"]["FNumber"].GetString();
                    if (ParentUnit.Equals("tao"))
                        jobject["Model"]["FSaleOrderEntry"][i]["FUNITID"]["FNumber"] = "tao1";
                    string FPriceUnitId = jobject["Model"]["FSaleOrderEntry"][i]["FPriceUnitId"]["FNumber"].GetString();
                    if (FPriceUnitId.Equals("㎡") || FPriceUnitId.Equals("m2") || FPriceUnitId.Equals("㎡"))
                        jobject["Model"]["FSaleOrderEntry"][i]["FPriceUnitId"]["FNumber"] = "m²";
                    string[] colorArray = jobject["Model"]["FSaleOrderEntry"][i]["FAuxPropId"]["FAUXPROPID__FF100006"].ToString().Split('内');
                    if (colorArray.Length > 1)
                    {
                        jobject["Model"]["FSaleOrderEntry"][i]["F_OutColor"] = colorArray[0].Replace("外", "");
                        jobject["Model"]["FSaleOrderEntry"][i]["F_InColor"] = colorArray[1];
                    }
                    else
                    {
                        jobject["Model"]["FSaleOrderEntry"][i]["F_InColor"] = colorArray[0];
                        jobject["Model"]["FSaleOrderEntry"][i]["F_OutColor"] = colorArray[0];
                    }
                    string bomid = jobject["Model"]["FSaleOrderEntry"][i]["FBomId"]["FNumber"].ToString();
                    var FBomId = new JProperty("FBomId", JsonConvert.DeserializeObject("{\"FNumber\":\"" + bomid + "\"}"));
                    var BOM = new JProperty("F_BomVer", bomid);

                    jobject["Model"]["FSaleOrderEntry"][i]["FBomId"].Parent.Remove();

                    jobject["Model"]["FSaleOrderEntry"][i].Last.AddAfterSelf(FBomId);
                    jobject["Model"]["FSaleOrderEntry"][i].Last.AddAfterSelf(BOM);

                    var sql = string.Format("UPDATE T_ENG_BOM SET F_SALESNO='{0}' WHERE FNUMBER='{1}'", salesno, bomid);
                    DBUtils.Execute(Context, sql);
                    sql = string.Format("SELECT FNUMBER  FROM T_ORG_ORGANIZATIONS WHERE FORGID IN (SELECT F_KUCUNZUZHI FROM T_BD_MATERIAL where FNUMBER='{0}')", material);
                    DataSet ds = DBUtils.ExecuteDataSet(this.Context, sql);
                    string forgid = ds.Tables[0].Rows[0]["FNUMBER"].ToString();
                    jobject["Model"]["FSaleOrderEntry"][i]["FStockOrgId"]["FNumber"] = forgid;
                    jobject["Model"]["FSaleOrderEntry"][i]["FSupplyOrgId"]["FNumber"] = forgid;
                }

                //序列化后覆盖RequestDTO
                e.WebContext.RequestDTO = JsonConvert.SerializeObject(jobject);
            }
        }
    }
}
